package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.HomeActivity;
import com.xkdx.guangguang.fragment.home.HomeFragment;
import com.xkdx.guangguang.fragment.home.HomeModule.HomeInfo;
import com.xkdx.guangguang.fragment.home.homedetail.HomeDetailFragment;
import com.xkdx.guangguang.wiget.CustomListView;

public class HomeAdapter extends BaseAdapter {
	private List<HomeInfo> list;
	private LayoutInflater inflater;
	private boolean isFirst;
	int height = 0;
	HomeActivity context;
	HomeFragment homeFragment;
	ListView list1, list2, list3;

	public HomeAdapter(Context context, List<HomeInfo> list, boolean isFirst,
			ListView list1, ListView list2, ListView list3) {
		this.context = (HomeActivity) context;
		this.isFirst = isFirst;
		this.list = list;
		this.list1 = list1;
		this.list2 = list2;
		this.list3 = list3;
		this.inflater = (LayoutInflater) context
				.getSystemService("layout_inflater");
		FragmentManager fm = ((HomeActivity) context)
				.getSupportFragmentManager();

		homeFragment = (HomeFragment) fm.findFragmentByTag("homeFragment");

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.home_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.txtTitle = (TextView) convertView
					.findViewById(R.id.textView1);
			viewHolder.txtDesc = (TextView) convertView
					.findViewById(R.id.textView2);
			viewHolder.txtTime = (TextView) convertView.findViewById(R.id.time);
			viewHolder.ll = (LinearLayout) convertView
					.findViewById(R.id.home_list);
			viewHolder.iv = (ImageView) convertView
					.findViewById(R.id.home_right);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (isFirst == false) {
			viewHolder.txtTitle.setTextColor(Color.WHITE);
			viewHolder.txtDesc.setTextColor(Color.WHITE);
			viewHolder.txtTime.setTextColor(Color.WHITE);
			viewHolder.iv.setVisibility(View.INVISIBLE);
			viewHolder.ll.setBackgroundResource(R.drawable.home_list_bg);
			viewHolder.ll.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					list1.setSelection(position);
					list2.setSelection(position);
					list3.setSelection(position);
				}
			});
			viewHolder.ll.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						((CustomListView) list1).startTouch();
					}
					return false;
				}
			});

		} else {
			viewHolder.txtTitle.setTextColor(Color.BLACK);
			viewHolder.txtDesc.setTextColor(Color.BLACK);
			viewHolder.txtTime.setTextColor(Color.BLACK);
			viewHolder.ll.setBackgroundResource(android.R.color.white);
			viewHolder.ll.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					FragmentTransaction ft = homeFragment.getFragmentManager()
							.beginTransaction();
					HomeDetailFragment homeDetailFragment = new HomeDetailFragment();
					ft.replace(R.id.fragment, homeDetailFragment);
					Bundle b = new Bundle();
					homeDetailFragment.setArguments(b);
					b.putString("infoID", list.get(position).getInfoID());
					ft.addToBackStack(null);
					ft.commit();
				}
			});
			viewHolder.ll.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						((CustomListView) list2).startTouch();
					}
					return false;
				}
			});
		}

		viewHolder.txtTitle.setText(list.get(position).getInfoTitle());
		viewHolder.txtDesc.setText(!("".equals(list.get(position)
				.getSimpleIntro())) ? list.get(position).getSimpleIntro()
				: "暂无简介");

		// 新接口调用
		viewHolder.txtTime.setText(list.get(position).getUpdateTime()
				.substring(0, 10));

		return convertView;
	}

	private class ViewHolder {
		LinearLayout ll;
		ImageView iv;
		TextView txtTitle;
		TextView txtDesc, txtTime;
	}

}
